package cn.com.cc.log;

import org.apache.log4j.Level;
import org.apache.log4j.Priority;



public class Logger {
	
	private static org.apache.log4j.Logger logger = null;
	
	private static final String FQCN = Logger.class.getName();
	
	public static boolean ENABLE_DEBUG = false;

	public static boolean ENABLE_INFO = false;

	public static boolean ENABLE_WARN = false;

	public static boolean ENABLE_ERROR = false;
	
	
	public static void initLog(org.apache.log4j.Logger log){
		logger = log;
		ENABLE_DEBUG = Level.DEBUG.isGreaterOrEqual(log.getEffectiveLevel());
		ENABLE_INFO = Level.INFO.isGreaterOrEqual(log.getEffectiveLevel());
		ENABLE_WARN = Level.WARN.isGreaterOrEqual(log.getEffectiveLevel());
		ENABLE_ERROR = Level.ERROR.isGreaterOrEqual(log.getEffectiveLevel());
	}
	
	/**
	 * Message format
	 * 
	 * @param msg
	 *            Message
	 * @return
	 */
	private static String formatMsg(String msg) {
		if (msg == null){
			msg = "null";
		}
		return (" 信息提示--> ".concat(msg));
	}

	/**
	 * Message format
	 * 
	 * @param msg
	 *            Message
	 * @param e
	 *            Exception
	 * @return
	 */
	private static String formatMsg(String msg, Throwable e) {
		StackTraceElement[] ste = e.getStackTrace();
		if (ste != null && ste.length > 0) {
			return (":" + ste[0].getClassName() + " - " + msg);
		}
		return formatMsg(msg);
	}

	/**
	 * Logger a message with the DEBUG level.
	 * 
	 * @param msg
	 *            Message
	 */
	public static void debug(String msg) {
		if (ENABLE_DEBUG) {
			logger.log(FQCN, Level.DEBUG, formatMsg(msg), null);
		}
	}

	public static void debug(String msg, Throwable t) {
		if (ENABLE_DEBUG) {
			logger.log(FQCN, Level.DEBUG, formatMsg(msg), t);
		}
	}

	/**
	 * Logger a message object with the ERROR Level.
	 * 
	 * @param msg
	 *            Message
	 */
	public static void error(String msg, Throwable t) {
		if (ENABLE_ERROR) {
			logger.log(FQCN, Level.ERROR, formatMsg(msg), t);
		}
	}

	/**
	 * Logger a message object with the ERROR Level.
	 * 
	 * @param msg
	 *            Message
	 */
	public static void error(String msg) {
		if (ENABLE_ERROR) {
			logger.log(FQCN, Level.ERROR, formatMsg(msg), null);
		}
	}

	/**
	 * Logger a message object with the FATAL Level.
	 * 
	 * @param msg
	 *            Message
	 */
	public static void fatal(String msg) {
		logger.log(FQCN, Level.FATAL, formatMsg(msg), null);
	}
	
	public static void fatal(String msg, Throwable t) {
		logger.log(FQCN, Level.FATAL, formatMsg(msg), t);
	}

	/**
	 * Logger a message object with the INFO Level.
	 * 
	 * @param msg
	 *            Message
	 */
	public static void info(String msg) {
		if (ENABLE_INFO) {
			logger.log(FQCN, Level.INFO, formatMsg(msg), null);
		}
	}

	public static void info(String msg, Throwable t) {
		if (ENABLE_INFO) {
			logger.log(FQCN, Level.INFO, formatMsg(msg), t);
		}
	}

	/**
	 * Logger a message object with the WARN Level.
	 * 
	 * @param msg
	 *            Message
	 */
	public static void warn(String msg) {
		if (ENABLE_WARN) {
			logger.log(FQCN, Level.WARN, formatMsg(msg), null);
		}
	}

	public static void warn(String msg, Throwable t) {
		if (ENABLE_WARN) {
			logger.log(FQCN, Level.WARN, formatMsg(msg), t);
		}
	}

	/**
	 * Logger a message object with the costom level.
	 * 
	 * @param priority
	 *            Custom level include
	 * @param msg
	 *            Message
	 */
	public static void log(Priority priority, String msg) {
		logger.log(FQCN, priority, formatMsg(msg), null);
	}

	/**
	 * Logger a message object with the costom level.
	 * 
	 * @param priority
	 *            Custom level include
	 * @param msg
	 *            Message
	 * @param e
	 *            Exception
	 */
	public static void log(Priority priority, String msg, Throwable e,
			boolean exlogFlag) {
		logger.log(FQCN, priority, formatMsg(msg, e), exlogFlag ? e : null);
	}

	/**
	 * Write a message when start a method
	 * 
	 * @param sName
	 *            Method name
	 */
	public static void startMethod(String sName) {
		debug("Method ".concat(sName).concat(" start."));
	}

	/**
	 * Write a message when end a method
	 * 
	 * @param sName
	 *            Method name
	 */
	public static void endMethod(String sName) {
		debug("Method ".concat(sName).concat(" end."));
	}

}

